package com.ruoyi.web.controller.system;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.system.domain.PublicReview;
import com.ruoyi.system.service.PublicReviewService;
import com.ruoyi.system.service.WorkService;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.Hashtable;
import java.util.List;

/**
 * @Author 柳
 * @Date 2021/3/10
 * @Description TODO
 * 大众评审
 */
@RestController
@RequestMapping("/system/publicreview")
public class PublicReviewController {

    @Autowired
    private PublicReviewService publicReviewService;

    @Autowired
    private WorkService workService;



    @ApiOperation("查询用户已投票数")
    @GetMapping("/queryMaxVotePublicReview")
    public AjaxResult queryMaxVotePublicReview(PublicReview publicReview,HttpServletRequest request) {
        String ipAddr = IpUtils.getIpAddr(request);
        publicReview.setUser_ip(ipAddr);
        Integer count = publicReviewService.queryMaxVotePublicReview(publicReview);
        return AjaxResult.success(count);
    }



    /**
     * 添加大众评审信息
     */
    @PostMapping("/savePublicReview/{epid}")
    @ApiOperation("添加大众评审信息")
    public AjaxResult savePublicReview(@RequestBody List<String> ids , @PathVariable Integer epid, HttpServletRequest request) {
        if(ids.size()>3){
            return AjaxResult.error("不得超过可投票数！！！");
        }
        String ipAddr = IpUtils.getIpAddr(request);
        for (String id : ids) {
            PublicReview publicReview=new PublicReview();
            publicReview.setUser_ip(ipAddr);
            publicReview.setWork_id(id);
            publicReview.setEpid(epid);
            Integer count = publicReviewService.queryRepetitionPublicReview(publicReview);
            if(count>0){
                return AjaxResult.error("有重复投票请重新选择！！！");
            }
            Integer maxcount = publicReviewService.queryMaxVotePublicReview(publicReview);
            if(maxcount>2){
                return AjaxResult.error("不得超过可投票数！！！");
            }
        }
        for (String id : ids) {
            PublicReview publicReview=new PublicReview();
            publicReview.setUser_ip(ipAddr);
            publicReview.setWork_id(id);
            publicReview.setEpid(epid);
            publicReviewService.savePublicReview(publicReview);
            workService.updateVote(id);
        }
        return AjaxResult.success();
    }


}
